<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/NewsletterDomain.php';
class NewsletterDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, NewsletterDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO newsletter(newsletterID,email)
					VALUES	(
								".$obj->getNewsletterID().",
								'".$obj->getEmail()."'
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM newsletter WHERE newsletterID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE newsletter
				SET
					newsletter.email = '".$obj->getEmail()."'
				 WHERE newsletterID=".$obj->getNewsletterID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$newsletterDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM newsletter WHERE newsletter.newsletterID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$newsletterDomain = new NewsletterDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $newsletterDomain;
	}

	public function findAll()
	{
		$newsletterDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM newsletter";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$newsletterDomain = new NewsletterDomain($data);
				$newsletterDomainArr[] = $newsletterDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $newsletterDomain;
	}

}
?>